<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="基地分类" prop="subject">
        <el-select
          v-model="queryParams.bcid"
          placeholder="请选择基地分类"
          filterable
        >
          <el-option
            v-for="item in baseCateList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="基地名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入基地名称"
          clearable
        />
      </el-form-item>

      <el-form-item label="所属省/市" label-width="80px">
        <div class="m-4">
          <el-cascader
            v-model="queryAddress"
            :options="addressList"
            @change="handleChange"
            filterable
            @keyup.enter="handleQuery"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['place:place:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['place:place:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['place:place:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['place:place:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Grid" @click="toCategory"
          >基地分类管理</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain v-if="isOk" @click="openMy"
          >我负责的基地</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain v-if="isOk" @click="toMenu"
          >我负责的基地菜品管理</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="placeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="基地id" align="center" prop="id" />
      <el-table-column label="所属分类" align="center" prop="category" />
      <el-table-column label="基地名称" align="center" prop="name" />
      <el-table-column label="所属位置" align="center" prop="address" />
      <el-table-column label="基地具体地址" align="center" prop="position" />
      <el-table-column
        label="是否为热门推荐"
        align="center"
        prop="hot"
        width="250px"
      >
        <template #default="scope">
          <el-switch @change="changStatus(scope.row)" v-model="scope.row.hot" />
        </template>
      </el-table-column>
      <el-table-column label="基地图片" align="center" prop="image" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column
        label="基地菜品管理"
        align="center"
        prop="image"
        width="150"
      >
        <template #default="scope">
          <router-link
            :to="'/place/menu/index/' + scope.row.id"
            class="link-type"
          >
            <span v-hasPermi="['merchant:dish:all']">查看</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['place:place:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['place:place:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基地管理对话框 -->
    <el-dialog :title="title" v-model="open" width="1400px" append-to-body>
      <el-form ref="placeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属分类" prop="category">
          <el-select v-model="form.bcid" placeholder="请选择分类" filterable>
            <el-option
              v-for="item in baseCateList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="基地名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入基地名称" />
        </el-form-item>

        <el-form-item label="基地介绍" prop="info">
          <el-input
            v-model="form.info"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="地址所属省/市">
          <div class="m-4">
            <el-cascader
              v-model="address"
              :options="addressList"
              @change="handleChange"
              filterable
            />
          </div>
        </el-form-item>
        <el-form-item label="基地具体地址" prop="name">
          <el-input v-model="form.position" placeholder="请输入基地具体地址" />
        </el-form-item>

        <el-form-item label="基地图片" prop="image">
          <image-upload v-model="form.image" />
        </el-form-item>

        <el-form-item label="基地负责人" prop="username">
          <el-select
            v-model="form.username"
            placeholder="请选择负责人"
            filterable
          >
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userName"
            />
          </el-select>
        </el-form-item>

        <el-divider content-position="center">课程管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddCourse"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteCourse"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="courseList"
          :row-class-name="rowCourseIndex"
          @selection-change="handleCourseSelectionChange"
          ref="course"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="课程编码" prop="code" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.code" placeholder="请输入课程编码" />
            </template>
          </el-table-column>

          <el-table-column label="课程名称" prop="name" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.name" placeholder="请输入课程名称" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="价格" prop="price" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.price" placeholder="请输入价格" />
            </template>
          </el-table-column> -->
          <el-table-column label="适用人群" prop="applicablePerson" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.applicablePerson"
                placeholder="请输入适用人群"
              />
            </template>
          </el-table-column>
          <el-table-column label="课程介绍" prop="info" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.info" placeholder="请输入课程介绍" />
            </template>
          </el-table-column>
          <el-table-column label="上传图片" prop="image" width="350">
            <template #default="scope">
              <image-upload v-model="scope.row.image" />
            </template>
          </el-table-column>
          <el-table-column label="课程介绍" prop="location" width="350">
            <template #default="scope">
              <file-upload v-model="scope.row.location" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Place">
function toMenu() {
  router.push("/place/my/index/" + qid.value);
}

const qid = ref(-1);
const loadMyPlace = (flag) => {
  getMyPlace().then((response) => {
    if (response != "") {
      isOk.value = true;
    }
    console.log(isOk.value);
    form.value = response;
    qid.value = response.id;
    address.value = [response.proId, response.citId];
    courseList.value = response.courseList;
    open.value = flag;
  });
};
const openMy = () => {
  loadMyPlace(true);
};
const isOk = ref(false);
onMounted(() => {
  loadMyPlace(false);
});
import { listUser } from "@/api/system/user";
// 查询基地列表
const userList = ref([]);
const curUsername = ref("");
function getAllUserList() {
  listUser(loadAllParams).then((response) => {
    userList.value = response.rows;
  });
}
getAllUserList();

function changStatus(row) {
  data.form = row;
  address.value = [row.proId, row.citId];
  // console.log(data.form.status);
  // data.form.status=data.form.status=="true"?1:0;
  // console.log(data.form.status);
  console.log(data.form);

  submitForm();
}

// 查询基地列表
const addressList = ref([]);
const address = ref("");
function getAddressList() {
  getAllAddress().then((response) => {
    addressList.value = response;
  });
}
getAddressList();

import { listBaseCate } from "@/api/baseCate/baseCate";
import { loadAllParams } from "@/api/page";
// 查询基地列表
const baseCateList = ref([]);
function getBaseCateList() {
  listBaseCate(loadAllParams).then((response) => {
    baseCateList.value = response.rows;
  });
}
getBaseCateList();

import { useRoute, useRouter } from "vue-router";
const router = useRouter();
const toCategory = () => {
  router.push("/place/base_cate");
};

import {
  getMyPlace,
  getAllAddress,
  listPlace,
  getPlace,
  delPlace,
  addPlace,
  updatePlace,
  updatePlaceMy,
} from "@/api/place/place";
import { onMounted } from "vue";

const { proxy } = getCurrentInstance();

const placeList = ref([]);
const courseList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedCourse = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const queryAddress = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    price: null,
    name: null,
    citId: null,
    proId: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询基地管理列表 */
function getList() {
  loading.value = true;
  getBaseCateList();
  listPlace(queryParams.value).then((response) => {
    placeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    image: null,
    info: null,
    price: null,
    name: null,
    createTime: null,
    updateTime: null,
    bcid: null,
  };

  courseList.value = [];
  proxy.resetForm("placeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.proId = queryAddress.value[0];
  queryParams.value.citId = queryAddress.value[1];

  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  address.value = "";
  reset();
  open.value = true;
  title.value = "添加基地管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  address.value = [row.proId, row.citId];
  getPlace(_id).then((response) => {
    form.value = response.data;
    courseList.value = response.data.courseList;
    open.value = true;
    title.value = "修改基地管理";
  });
}

/** 提交按钮 */
function submitForm() {
  form.value.courseList = courseList.value;
  form.value.proId = address.value[0];
  form.value.citId = address.value[1];
  if (form.value.id != null) {
    if (qid.value == form.value.id) {
      updatePlaceMy(form.value).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        reset();
        getList();
      });
    } else {
      updatePlace(form.value).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        reset();
        getList();
      });
    }
  } else {
    addPlace(form.value).then((response) => {
      proxy.$modal.msgSuccess("新增成功");
      open.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除基地管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delPlace(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 课程管理序号 */
function rowCourseIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 课程管理添加按钮操作 */
function handleAddCourse() {
  let obj = {};
  obj.code = "";
  obj.subject = "";
  obj.name = "";
  obj.price = "";
  obj.applicablePerson = "";
  obj.info = "";
  obj.image = "";
  obj.location = "";
  courseList.value.push(obj);
}

/** 课程管理删除按钮操作 */
function handleDeleteCourse() {
  if (checkedCourse.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的课程管理数据");
  } else {
    const courses = courseList.value;
    const checkedCourses = checkedCourse.value;
    courseList.value = courses.filter(function (item) {
      return checkedCourses.indexOf(item.index) == -1;
    });
  }
}

/** 复选框选中数据 */
function handleCourseSelectionChange(selection) {
  checkedCourse.value = selection.map((item) => item.index);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "place/place/export",
    {
      ...queryParams.value,
    },
    `place_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>